-- MapSizeMenu
-- 地图选择菜单
MapSizeMenu = class()

function MapSizeMenu:init()
    self.mapSize = 0
    self.show = false
    self.smallButton = TextButton(WIDTH * 0.25,HEIGHT * 0.6,LP.MapSize.Small)
    self.smallButton.width = 300
    self.smallButton.height = 70
    self.smallButton.font = "Futura-Medium"
    self.smallButton.fontSize = 40
    self.smallButton.clicked = function() self:selected(SmallMap) end
    self.smallButton.backColor = color(255,255,255,15)
    self.smallButton.textColor = color(255,255,255,255)
    self.smallButton.pressedBackColor = color(255,255,255,15)
    
    self.normalButton = TextButton(WIDTH * 0.25,HEIGHT * 0.45,LP.MapSize.Normal)
    self.normalButton.width = 300
    self.normalButton.height = 70
    self.normalButton.font = "Futura-Medium"
    self.normalButton.fontSize = 40
    self.normalButton.clicked = function() self:selected(NormalMap) end
    self.normalButton.backColor = color(255,255,255,15)
    self.normalButton.textColor = color(255,255,255,255)
    self.normalButton.pressedBackColor = color(255,255,255,15)
    
    self.largeButton = TextButton(WIDTH * 0.25,HEIGHT * 0.3,LP.MapSize.Infinity)
    self.largeButton.width = 300
    self.largeButton.height = 70
    self.largeButton.font = "Futura-Medium"
    self.largeButton.fontSize = 40
    self.largeButton.clicked = function() self:selected(InfinityMap) end
    self.largeButton.backColor = color(255,255,255,15)
    self.largeButton.textColor = color(255,255,255,255)
    self.largeButton.pressedBackColor = color(255,255,255,15)
    
    self.backButton = TextButton(55,HEIGHT - 40,LP.Back)
    self.backButton.font = "Futura-Medium"
    self.backButton.fontSize = 30
    self.backButton.clicked = function() if self.advancedMode then self.advancedMode = false else self.show = false end end
    self.backButton.backColor = color(255,255,255,0)
    self.backButton.textColor = color(255,255,255,255)
    
    self.advancedButton = TextButton(WIDTH - 50,HEIGHT - 40,LP.Advanced)
    self.advancedButton.font = "Futura-Medium"
    self.advancedButton.fontSize = 30
    self.advancedButton.clicked = function() self.advancedMode = true end
    self.advancedButton.backColor = color(255,255,255,0)
    self.advancedButton.textColor = color(255,255,255,255)
    
    self.loadButton = TextButton(WIDTH * 0.75,HEIGHT * 0.28,LP.LoadMap)
    self.loadButton.width = 300
    self.loadButton.height = 70
    self.loadButton.font = "Futura-Medium"
    self.loadButton.fontSize = 40
    self.loadButton.clicked = function() self.show = false loadScreen = LoadingScreen(true) end
    self.loadButton.backColor = color(255,255,255,15)
    self.loadButton.textColor = color(255,255,255,255)
    self.loadButton.pressedBackColor = color(255,255,255,15)
    
    self.deleteButton = TextButton(WIDTH * 0.75,HEIGHT * 0.15,LP.DeteteMap)
    self.deleteButton.width = 300
    self.deleteButton.height = 70
    self.deleteButton.font = "Futura-Medium"
    self.deleteButton.fontSize = 40
    self.deleteButton.clicked = function() 
        SaveManager.deleteMap("Documents:TCSave")
        self.canLoadMap = nil
    end
    self.deleteButton.backColor = color(255,255,255,15)
    self.deleteButton.textColor = color(255,255,255,255)
    self.deleteButton.pressedBackColor = color(255,255,255,15)
    
    self.flatTerrainButton = TextButton(100,HEIGHT * 0.6,LP.FlatTerrain..LP.Disabled)
    self.flatTerrainButton.posMode = UIConst.PosMode.Corner
    self.flatTerrainButton.width = 300
    self.flatTerrainButton.height = 70
    self.flatTerrainButton.font = "Futura-Medium"
    self.flatTerrainButton.fontSize = 40
    self.flatTerrainButton.clicked = function() 
        self.flat = not self.flat
        if self.flat then
            self.flatTerrainButton.capital = LP.FlatTerrain..LP.Enabled
        else
            self.flatTerrainButton.capital = LP.FlatTerrain..LP.Disabled
        end
    end
    self.flatTerrainButton.backColor = color(255,255,255,15)
    self.flatTerrainButton.textColor = color(255,255,255,255)
    self.flatTerrainButton.pressedBackColor = color(255,255,255,15)
    
    self.advancedMode = false
    self.seed = self:genRandomSeed()
    self.flat = false
end

function MapSizeMenu:draw()
    self.canLoadMap = self.canLoadMap or worldSaver:testFile("Documents:TCSave")
    smooth()
    fill(255, 255, 255, 100)
    rectMode(CORNER)
    strokeWidth(0)
    rect(0,HEIGHT - 90,WIDTH,HEIGHT)
    if self.advancedMode then
        fill(255, 255, 255, 255)
        font("Futura-Medium")
        fontSize(40)
        textMode(CORNER)
        text(LP.MapSeed..self.seed,100,HEIGHT * 0.7)
        self.flatTerrainButton:draw()
    else
        stroke(255, 255, 255, 160)
        strokeWidth(2)
        lineCapMode(SQUARE)
        line(WIDTH / 2,0,WIDTH / 2,HEIGHT - 90)
        fill(255, 255, 255, 255)
        font("Futura-Medium")
        fontSize(45)
        textMode(CENTER)
        text(LP.CreateNewMap,WIDTH * 0.25,HEIGHT * 0.75)
        self.smallButton:draw()
        self.normalButton:draw()
        self.largeButton:draw()
        self.advancedButton:draw()
        if self.canLoadMap then
            local wi = worldSaver:getWorldInfo("Documents:TCSave")
            fontSize(40)
            text(wi.name,WIDTH * 0.75,HEIGHT * 0.75)
            fontSize(30)
            textMode(CORNER)
            textWrapWidth(WIDTH * 0.45)
            local worldMode
            if wi.mode == WorldMode.Normal then
                worldMode = LP.NormalMap
            else
                worldMode = LP.FlatMap
            end
            local txt = LP.Size..wi.length.."x"..wi.length.."x"..wi.height.."\n"..LP.WorldMode..worldMode.."\n"..LP.CreatedTime..wi.createdTime.."\n"..LP.Seed..wi.seed.."\n"..LP.LastPlayed..wi.lastPlayed
            local _,h = textSize(txt)
            text(txt,WIDTH * 0.55,HEIGHT * 0.7 - h)
            self.loadButton:draw()
            self.deleteButton:draw()
        else
            fontSize(40)
            text(LP.NoSaveFile,WIDTH * 0.75,HEIGHT / 2)
        end
    end
    self.backButton:draw()
    noSmooth()
end

function MapSizeMenu:touched(touch)
    if self.advancedMode then
        self.flatTerrainButton:touched(touch)
    else
        if self.canLoadMap then 
            self.loadButton:touched(touch)
            self.deleteButton:touched(touch)
        end
        self.smallButton:touched(touch)
        self.normalButton:touched(touch)
        self.largeButton:touched(touch)
        self.advancedButton:touched(touch)
    end
    self.backButton:touched(touch)
end

function MapSizeMenu:getWorldInfo()
    local wi
    if self.mapSize == SmallMap then wi = WorldInfo(64,64,_,self.seed)
    elseif self.mapSize == NormalMap then wi = WorldInfo(128,64,_,self.seed)
    elseif self.mapSize == LargeMap then wi = WorldInfo(256,64,_,self.seed)
    elseif self.mapSize == InfinityMap then wi = WorldInfo(4096,64,_,self.seed)
    end
    if self.flat then
        local sample = {}
        sample[1] = Block.bedRock.id
        for y = 2,10 do
            sample[y] = Block.stone.id
        end
        for y = 11,29 do
            sample[y] = Block.soil.id
        end
        sample[30] = Block.grass.id
        wi:setFlatSample(sample)
    end
    return wi
end

function MapSizeMenu:selected(mapSize)
    self.mapSize = mapSize
    self.show = false
    loadScreen = LoadingScreen()
end

function MapSizeMenu:genRandomSeed()
    local t = ""
    local w = math.random(4,9)
    for i = 1,w do
        t = t .. math.random(0,9)
    end
    return tonumber(t)
end
